<?php
class MenuController extends AdminAppController{
    
    function index($parent_id = null)
    {
        $this->loadModel('Admin.Menu');
        
        $menu = $this->Menu->get_all_menu('all', $parent_id);
        if($parent_id == null)
            $list_menu = $this->Menu->filter_menu($menu);
        else
            $list_menu = $menu;
        $this->set('list_menu', $list_menu);
        $parent_menu = $this->Menu->get_parent_menu();
        
        $this->set('parent_menu', $parent_menu);
        $this->set('parent_id', $parent_id);
    }
    
    function add($parent_id = null)
    {
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Menu');
        $this->loadModel('Admin.MenuDesc');
        $this->loadModel('Admin.Image');
        
        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        $menu = $this->Menu->get_all_menu('all');
        $list_menu = $this->Menu->filter_menu($menu);
        $this->set('list_menu', $list_menu);
        
        $this->set('parent_id', $parent_id);
        if($this->request->isPost())
        {
            $data = array(
                        'parent_id' => $this->request->data['parent_id'],
                        'menu_code' => $this->request->data['Menu']['menu_code'],
                        'other' => $this->request->data['Menu']['other'],
                        'status' => $this->request->data['status'],
                        'sort_order' => $this->request->data['Menu']['sort_order'],
                        );
            $menu_id = $this->Menu->add($data);
            
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'menu_id' => $menu_id,
                        'title' => $this->request->data['Menu']["title_$lang_code"],
                        'description' => $_POST["description_$lang_code"],
                        'link' => $this->request->data['Menu']["link_$lang_code"],
                        'lang_code' => $lang_code,
                        );
                $this->MenuDesc->add($data);
            }
            
            $main_image = $this->request->data['main_image'];

            $data['main_image'] = $main_image;
            $data['other_image'] = array();
            $data['order_image'] = array();
            $this->Image->add($menu_id, 'menu', $data);
           
            
            $this->Session->setFlash(
                                'Thêm mới menu thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "menu", 
                                  "action" => "index", $parent_id));
        }  
    }
    
    function edit($menu_id = "", $parent_id = null)
    {
        if($menu_id == ""){
            $this->redirect(array("controller" => "menu", 
                                  "action" => "index")
                            );
            exit;
        }
        $this->set('menu_id', $menu_id);
        $this->loadModel('Admin.Language');
        $this->loadModel('Admin.Menu');
        $this->loadModel('Admin.MenuDesc');
        $this->loadModel('Admin.Image');
        
        $menu_info = $this->Menu->get_menu_by_id($menu_id, 'all');
        $this->set('menu_info', $menu_info);

        $list_language = $this->Language->get_language();
        $this->set('list_language', $list_language);
        
        $list_image = $this->Image->get($menu_id, 'menu');
        $this->set('list_image', $list_image);
    
        $menu = $this->Menu->get_all_menu('all');
        $list_menu = $this->Menu->filter_menu($menu);
        $this->set('list_menu', $list_menu);
        
        $this->set('parent_id', $parent_id);
        if($this->request->isPost())
        {
            $data = array(
                        'parent_id' => $this->request->data['parent_id'],
                        'menu_code' => "'".$this->request->data['Menu']['menu_code']."'",
                        'other' => "'".$this->request->data['Menu']['other']."'",
                        'status' => "'".$this->request->data['status']."'",
                        'sort_order' => "'".$this->request->data['Menu']['sort_order']."'",
                        );
            $this->Menu->edit($menu_id, $data);
            
            foreach($list_language as $language)
            {
                $language = $language['language'];
                $lang_code = $language['lang_code'];
                $data = array(
                        'title' => "'".$this->request->data['Menu']["title_$lang_code"]."'",
                        'link' => "'".$this->request->data['Menu']["link_$lang_code"]."'",
                        'description' => "'".$_POST["description_$lang_code"]."'",
                        );
                $this->MenuDesc->edit($menu_id, $lang_code, $data);
            }
            
            $main_image = $this->request->data['main_image'];
            $data['main_image'] = $main_image;
            $data['other_image'] = array();
            $data['order_image'] = array();
            $this->Image->edit($menu_id, 'menu', $data);
            
            $this->Session->setFlash(
                                'Sửa menu thành công',
                                'default',
                                array('class' => 'alert alert-success')
                            );
            
            $this->redirect(array("controller" => "menu", 
                                  "action" => "index", $parent_id));
        } 
    }
}